package org.testis.gui.mvc.view.testdesign;

import org.testis.gui.mvc.view.*;
import org.testis.gui.mvc.controller.TestDesignControllerList;
import org.testis.gui.mvc.controller.testdesign.ConditionsController;
import org.testis.gui.mvc.utilities.Validatable;
import org.testis.gui.mvc.controller.testdesign.TestPlanController;
import org.testis.gui.mvc.utilities.AtofEntryItem;
import org.testis.gui.mvc.utilities.ValidationUtils;
import org.testis.orm.testbed.refined.DeviceGroupRefined;
import org.testis.orm.testbed.refined.DutRefined;
import org.testis.orm.testbed.refined.TestPcRefined;
import java.awt.Frame;
import java.awt.event.WindowListener;
import java.beans.PropertyChangeEvent;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import org.orm.PersistentException;

/**
 *
 * @author  Cahit Emir Erel
 */
public class TestPlanView extends AtofBaseDialog implements
        AbstractViewPanel, Validatable {

    private static final long serialVersionUID = 7033414L;
    private String m_windowTitle;
    private boolean m_editMode;
    HashMap<Integer, String> hLanguages;
    HashMap<Integer, String> hTestBeds;
    HashMap<Integer, String> hProfiles;
    HashMap<Integer, String> hTestPcs;
    HashMap<Integer, String> hDuts;

    /** Creates new form AddTestPlanView */
    public TestPlanView(Frame parent,
            boolean modal,
            boolean editMode) {
        super(parent,
                modal);
        m_editMode = editMode;
        if (editMode) {
            m_windowTitle = "Edit Test Plan";
        } else {
            m_windowTitle = "Add Test Plan";
        }
        initComponents();
        loadTestBeds();
        loadPcs();
        loadProfiles();
        cmbDutList.removeAllItems();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        saveButton = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel2 = new javax.swing.JPanel();
        nameLabel = new javax.swing.JLabel();
        nameTextField = new javax.swing.JTextField();
        descriptionLabel = new javax.swing.JLabel();
        descriptionTextField = new javax.swing.JTextField();
        timeoutLabel = new javax.swing.JLabel();
        timeoutTextField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        cmbTestBeds = new javax.swing.JComboBox();
        cmbLanguages = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        cmbProfiles = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jPanel4 = new javax.swing.JPanel();
        browserSpeedSpinner = new JSpinner(new SpinnerNumberModel(600, //initial value
            100, //min
            2000, //max
            50));
    dutLabel = new javax.swing.JLabel();
    browserTypeLabel = new javax.swing.JLabel();
    browserSpeedLabel = new javax.swing.JLabel();
    pcLabel = new javax.swing.JLabel();
    browserTypeChoice = new java.awt.Choice();
    dutLabel1 = new javax.swing.JLabel();
    cmbPcList = new javax.swing.JComboBox();
    cmbDutList = new javax.swing.JComboBox();
    txtDutIp = new javax.swing.JTextField();
    jSeparator1 = new javax.swing.JSeparator();
    closeButton = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
    setTitle(m_windowTitle);
    setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    setResizable(false);
    addWindowListener(new java.awt.event.WindowAdapter() {
        public void windowClosed(java.awt.event.WindowEvent evt) {
            formWindowClosed(evt);
        }
    });

    String label;
    if (m_editMode) {
        label = "Save Changes";
    } else {
        label = "Add Test Plan";
    }
    saveButton.setText("Save");
    saveButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            saveButtonActionPerformed(evt);
        }
    });

    nameLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    nameLabel.setLabelFor(nameTextField);
    nameLabel.setText("Name * :");

    nameTextField.setFont(new java.awt.Font("Dialog", 0, 11));
    nameTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            nameTextFieldActionPerformed(evt);
        }
    });
    nameTextField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            nameTextFieldFocusLost(evt);
        }
    });

    descriptionLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    descriptionLabel.setLabelFor(descriptionTextField);
    descriptionLabel.setText("Description :");

    descriptionTextField.setFont(new java.awt.Font("Dialog", 0, 11));
    descriptionTextField.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            descriptionTextFieldActionPerformed(evt);
        }
    });
    descriptionTextField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusLost(java.awt.event.FocusEvent evt) {
            descriptionTextFieldFocusLost(evt);
        }
    });

    timeoutLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    timeoutLabel.setLabelFor(timeoutTextField);
    timeoutLabel.setText("Timeout :");

    timeoutTextField.setEditable(false);
    timeoutTextField.setFont(new java.awt.Font("Dialog", 0, 11));

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(descriptionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 63, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(timeoutLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(timeoutTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addComponent(descriptionTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                .addComponent(nameTextField, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE))
            .addContainerGap())
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(nameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(nameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGap(7, 7, 7)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(descriptionLabel)
                .addComponent(descriptionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(timeoutLabel)
                .addComponent(timeoutTextField))
            .addGap(421, 421, 421))
    );

    jTabbedPane1.addTab("General", jPanel2);

    cmbTestBeds.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    cmbTestBeds.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            cmbTestBedsİtemStateChanged(evt);
        }
    });

    cmbLanguages.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    cmbLanguages.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            cmbLanguagesItemStateChanged(evt);
        }
    });

    jLabel1.setText("Language:");

    cmbProfiles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    cmbProfiles.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            cmbProfilesİtemStateChanged(evt);
        }
    });

    jLabel3.setText("Test Bed * :");

    jLabel2.setText("Profile:");

    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout.setHorizontalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jLabel1)
                .addComponent(jLabel2)
                .addComponent(jLabel3))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 58, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                .addComponent(cmbTestBeds, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cmbProfiles, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(cmbLanguages, 0, 280, Short.MAX_VALUE))
            .addContainerGap())
    );

    jPanel1Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jLabel1, jLabel2, jLabel3});

    jPanel1Layout.setVerticalGroup(
        jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel1Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel1)
                .addComponent(cmbLanguages, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel2)
                .addComponent(cmbProfiles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(jLabel3)
                .addComponent(cmbTestBeds, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap(38, Short.MAX_VALUE))
    );

    browserSpeedSpinner.addChangeListener(new javax.swing.event.ChangeListener() {
        public void stateChanged(javax.swing.event.ChangeEvent evt) {
            browserSpeedSpinnerStateChanged(evt);
        }
    });

    dutLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    dutLabel.setText("DUT * :");

    browserTypeLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    browserTypeLabel.setLabelFor(browserTypeChoice);
    browserTypeLabel.setText("Browser Type * :");

    browserSpeedLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    browserSpeedLabel.setText("Browser Speed * :");

    pcLabel.setFont(new java.awt.Font("Dialog", 0, 11));
    pcLabel.setText("PC Name * :");

    browserTypeChoice.add("");
    String[] choices = TestDesignControllerList.newInstance().getTestPlanController().operationGetBrowserTypes();
    for (int i=0;i<choices.length;i++) {
        browserTypeChoice.add(choices[i]);
    }
    browserTypeChoice.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            browserTypeChoiceItemStateChanged(evt);
        }
    });

    dutLabel1.setFont(new java.awt.Font("Dialog", 0, 11));
    dutLabel1.setText("Dut IP :");

    cmbPcList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    cmbPcList.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            cmbPcListİtemStateChanged(evt);
        }
    });

    cmbDutList.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
    cmbDutList.addItemListener(new java.awt.event.ItemListener() {
        public void itemStateChanged(java.awt.event.ItemEvent evt) {
            cmbDutListİtemStateChanged(evt);
        }
    });

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel4Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addComponent(browserTypeLabel)
                    .addGap(27, 27, 27)
                    .addComponent(browserTypeChoice, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(browserSpeedLabel)
                        .addComponent(dutLabel)
                        .addComponent(pcLabel)
                        .addComponent(dutLabel1))
                    .addGap(27, 27, 27)
                    .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(browserSpeedSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmbPcList, 0, 279, Short.MAX_VALUE)
                        .addComponent(cmbDutList, 0, 279, Short.MAX_VALUE)
                        .addComponent(txtDutIp, javax.swing.GroupLayout.DEFAULT_SIZE, 279, Short.MAX_VALUE))))
            .addContainerGap())
    );

    jPanel4Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {browserSpeedLabel, browserTypeLabel, dutLabel, pcLabel});

    jPanel4Layout.setVerticalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel4Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(browserTypeChoice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(browserTypeLabel))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                .addComponent(browserSpeedSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(browserSpeedLabel, javax.swing.GroupLayout.DEFAULT_SIZE, 24, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(pcLabel)
                .addComponent(cmbPcList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(dutLabel)
                .addComponent(cmbDutList, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(dutLabel1)
                .addComponent(txtDutIp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );

    javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
    jPanel3.setLayout(jPanel3Layout);
    jPanel3Layout.setHorizontalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 508, Short.MAX_VALUE)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addContainerGap())
    );

    jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {jPanel1, jPanel4});

    jPanel3Layout.setVerticalGroup(
        jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel3Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addContainerGap())
    );

    jTabbedPane1.addTab("Run-time parameters", jPanel3);

    closeButton.setText("Close");
    closeButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent evt) {
            closeButtonActionPerformed(evt);
        }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 533, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(layout.createSequentialGroup()
                    .addComponent(saveButton)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(closeButton)))
            .addContainerGap())
    );

    layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {closeButton, saveButton});

    layout.setVerticalGroup(
        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 362, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(closeButton)
                .addComponent(saveButton))
            .addContainerGap())
    );

    ConditionsController.createInstance(m_editMode).initialize();
    jTabbedPane1.addTab("Conditions", ConditionsController.getInstance().getConditionView());

    pack();
    }// </editor-fold>//GEN-END:initComponents

    private void loadProfiles() {
        cmbProfiles.removeAllItems();
        cmbProfiles.addItem("");
        hProfiles = TestDesignControllerList.newInstance().
                getProfileSettingsController().operationGetProfiles();
        if (hProfiles != null) {
            Iterator iter = hProfiles.entrySet().
                    iterator();
            while (iter.hasNext()) {
                cmbProfiles.addItem(new AtofEntryItem((Map.Entry) iter.next()));
            }
        }
        cmbProfiles.repaint();
        cmbProfiles.updateUI();
    }

    private void loadLanguages() {
        cmbLanguages.removeAllItems();
        cmbLanguages.addItem("");
        hLanguages = TestDesignControllerList.newInstance().
                getTestPlanController().operationGetLanguages();
        if (hLanguages != null) {
            Iterator iter = hLanguages.entrySet().
                    iterator();
            while (iter.hasNext()) {
                cmbLanguages.addItem(new AtofEntryItem((Map.Entry) iter.next()));
            }
        }
        cmbLanguages.repaint();
        cmbLanguages.updateUI();
    }

    private void loadTestBeds() {
        cmbTestBeds.removeAllItems();
        cmbTestBeds.addItem("");
        List<DeviceGroupRefined> deviceGroups = TestDesignControllerList.newInstance().
                getTestPlanController().operationGetTestBeds();
        if (deviceGroups != null) {
            for (int i = 0; i < deviceGroups.size(); i++) {
                if (hTestBeds == null) {
                    hTestBeds = new HashMap<Integer, String>();
                }
                hTestBeds.put(deviceGroups.get(i).getId(), deviceGroups.get(i).getName());
            }
            if (hTestBeds != null) {
                Iterator iter = hTestBeds.entrySet().
                        iterator();
                while (iter.hasNext()) {
                    cmbTestBeds.addItem(new AtofEntryItem((Map.Entry) iter.next()));
                }
            }
        }
        cmbTestBeds.repaint();
        cmbTestBeds.updateUI();
    }

    private void loadPcs() {
        cmbPcList.removeAllItems();
        cmbPcList.addItem("");
        List<TestPcRefined> pcs = TestDesignControllerList.newInstance().
                getTestPlanController().operationGetPcList();
        if (pcs != null) {
            if (hTestPcs == null) {
                hTestPcs = new HashMap<Integer, String>();
            } else {
                hTestPcs.clear();
            }
            for (int i = 0; i < pcs.size(); i++) {
                hTestPcs.put(pcs.get(i).getId(), pcs.get(i).getName());
            }
            if (hTestPcs != null) {
                Iterator iter = hTestPcs.entrySet().
                        iterator();
                while (iter.hasNext()) {
                    cmbPcList.addItem(new AtofEntryItem((Map.Entry) iter.next()));
                }
            }
        }
        cmbPcList.repaint();
        cmbPcList.updateUI();
    }

    private void nameTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nameTextFieldActionPerformed
        try {
            TestDesignControllerList.newInstance().getTestPlanController().
                    operationChangeName(nameTextField.getText());
        } catch (Exception e) {
            //  Handle exception
        }
    }//GEN-LAST:event_nameTextFieldActionPerformed

    private void nameTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_nameTextFieldFocusLost
        try {
            TestDesignControllerList.newInstance().getTestPlanController().
                    operationChangeName(nameTextField.getText());
        } catch (Exception e) {
            //  Handle exception
        }
    }//GEN-LAST:event_nameTextFieldFocusLost

    private void descriptionTextFieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descriptionTextFieldActionPerformed
        try {
            TestDesignControllerList.newInstance().getTestPlanController().
                    operationChangeDescription(descriptionTextField.getText());
        } catch (Exception e) {
            //  Handle exception
        }
}//GEN-LAST:event_descriptionTextFieldActionPerformed

    private void descriptionTextFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_descriptionTextFieldFocusLost
        try {
            TestDesignControllerList.newInstance().getTestPlanController().
                    operationChangeDescription(descriptionTextField.getText());
            //Integer.parseInt(widthTextField.getText()));
        } catch (Exception e) {
            //  Handle exception
        }
}//GEN-LAST:event_descriptionTextFieldFocusLost

    @Override
    public boolean validateForm() {
        boolean valid = true;
        StringBuilder builder = new StringBuilder();
        if (ValidationUtils.isEmpty(nameTextField.getText())) {
            builder.append("Name must not be empty.\n");

            valid = false;
        }
        if (cmbTestBeds.getSelectedItem() == null
                || ValidationUtils.isEmpty(cmbTestBeds.getSelectedItem().toString())) {
            builder.append("Test bed must not be empty.\n");

            valid = false;
        }
        if (cmbLanguages.getSelectedItem() == null
                || ValidationUtils.isEmpty(cmbLanguages.getSelectedItem().toString())) {
            builder.append("Language must not be empty.\n");

            valid = false;
        }
        if (cmbPcList.getSelectedItem() == null
                || ValidationUtils.isEmpty(cmbPcList.getSelectedItem().toString())) {
            builder.append("Pc must not be empty.\n");

            valid = false;
        }
        if (cmbDutList.getSelectedItem() == null
                || ValidationUtils.isEmpty(cmbDutList.getSelectedItem().toString())) {
            builder.append("Dut must not be empty.\n");

            valid = false;
        }
        if (ValidationUtils.isEmpty(browserTypeChoice.getSelectedItem())) {
            builder.append("Browser type must not be empty.\n");

            valid = false;
        }
        if (!valid) {
            JOptionPane.showMessageDialog(null,
                    builder.toString(),
                    "Error",
                    JOptionPane.ERROR_MESSAGE);
        }
        return valid;
    }

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed

        if (!validateForm()) {
            return;
        }
        saveButton.setEnabled(false);

        HashMap<String, Object> params = new HashMap<String, Object>();

        params.put("name",
                nameTextField.getText());
        params.put("description",
                descriptionTextField.getText());
        params.put("browserSpeed",
                ((JSpinner.NumberEditor) browserSpeedSpinner.getEditor()).getTextField().
                getText());
        params.put("browserType",
                "*" + browserTypeChoice.getSelectedItem());
        if (cmbPcList.getSelectedItem() != null && !cmbPcList.getSelectedItem().
                toString().equals("")) {
            Entry entry = (Entry) cmbPcList.getSelectedItem();
            if (entry.getKey() != null) {
                params.put("pc",
                        entry.getKey().toString());
            }
        }
        if (cmbDutList.getSelectedItem() != null && !cmbDutList.getSelectedItem().
                toString().equals("")) {
            Entry entry = (Entry) cmbDutList.getSelectedItem();
            if (entry.getKey() != null) {
                params.put("dut", entry.getKey().toString());
            }
        }
        if (cmbLanguages.getSelectedItem() != null && !cmbLanguages.getSelectedItem().
                toString().equals("")) {
            Entry entry = (Entry) cmbLanguages.getSelectedItem();
            params.put("languageId",
                    entry.getKey().toString());
        }
        if (cmbTestBeds.getSelectedItem() != null && !cmbTestBeds.getSelectedItem().
                toString().equals("")) {
            Entry entry = (Entry) cmbTestBeds.getSelectedItem();
            params.put("testBedId",
                    entry.getKey().toString());
        }

        if (cmbProfiles.getSelectedItem() != null && !cmbProfiles.getSelectedItem().
                toString().equals("")) {
            Entry entry = (Entry) cmbProfiles.getSelectedItem();
            params.put("profileId",
                    entry.getKey().toString());
        }
        if (txtDutIp.getText() != null && !txtDutIp.getText().equals("")) {
            params.put("deviceIp",
                    txtDutIp.getText());
        }
        LinkedHashMap<Integer, String> listOfCond = ConditionsController.getInstance().getConditionsList();


        if (listOfCond != null) {
            params.put("conditions", listOfCond);
        }
        if (m_editMode) {
            TestDesignControllerList.newInstance().getTestPlanController().
                    operationSaveTestPlan(params);
        } else {
            try {
                TestDesignControllerList.newInstance().getTestPlanController().operationAddTestPlan(params);
            } catch (PersistentException ex) {
                ex.printStackTrace();
            }
        }


        this.dispose();
    }//GEN-LAST:event_saveButtonActionPerformed

    private void browserTypeChoiceItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_browserTypeChoiceItemStateChanged

        TestDesignControllerList.newInstance().getTestPlanController().
                operationChangeBrowserType(browserTypeChoice.getSelectedItem());
}//GEN-LAST:event_browserTypeChoiceItemStateChanged

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed

        WindowListener[] listeners = getWindowListeners();
        for (int i = 0; i < listeners.length; i++) {
            this.removeWindowListener(listeners[i]);
        }
        this.setEnabled(false);
        //this.dispose();
        //this.invalidate();
        TestDesignControllerList.newInstance().getTestPlanController().
                operationCloseWindow();
        ConditionsController.createInstance(m_editMode).operationClose();
    }//GEN-LAST:event_formWindowClosed

    private void browserSpeedSpinnerStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_browserSpeedSpinnerStateChanged

        TestDesignControllerList.newInstance().getTestPlanController().
                operationChangeBrowserSpeed(((JSpinner.NumberEditor) browserSpeedSpinner.getEditor()).getTextField().
                getText());
    }//GEN-LAST:event_browserSpeedSpinnerStateChanged

private void cmbLanguagesItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbLanguagesItemStateChanged
    int langId = 0;
    if (cmbLanguages.getSelectedItem() != null && !cmbLanguages.getSelectedItem().
            toString().equals("")) {
        Entry entry = (Entry) cmbLanguages.getSelectedItem();
        langId = Integer.parseInt(entry.getKey().toString());
    }
    TestDesignControllerList.newInstance().getTestPlanController().
            operationChangeLanguageId(langId);
}//GEN-LAST:event_cmbLanguagesItemStateChanged

private void closeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_closeButtonActionPerformed
    this.dispose();
}//GEN-LAST:event_closeButtonActionPerformed

private void cmbPcListİtemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbPcListİtemStateChanged
    int pcId = 0;
    if (cmbPcList.getSelectedItem() != null && !cmbPcList.getSelectedItem().
            toString().equals("")) {
        Entry entry = (Entry) cmbPcList.getSelectedItem();
        pcId = Integer.parseInt(entry.getKey().toString());
    }
    TestDesignControllerList.newInstance().getTestPlanController().
            operationChangePc(pcId);
}//GEN-LAST:event_cmbPcListİtemStateChanged

private void cmbDutListİtemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbDutListİtemStateChanged
    int dutId = 0;
    if (cmbDutList.getSelectedItem() != null && !cmbDutList.getSelectedItem().
            toString().equals("")) {
        Entry entry = (Entry) cmbDutList.getSelectedItem();
        dutId = Integer.parseInt(entry.getKey().toString());
    }
    TestDesignControllerList.newInstance().getTestPlanController().
            operationChangeDut(dutId);
}//GEN-LAST:event_cmbDutListİtemStateChanged

private void cmbProfilesİtemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbProfilesİtemStateChanged
    int profileId = 0;
    if (cmbProfiles.getSelectedItem() != null && !cmbProfiles.getSelectedItem().
            toString().equals("")) {
        Entry entry = (Entry) cmbProfiles.getSelectedItem();
        profileId = Integer.parseInt(entry.getKey().toString());
    }
    TestDesignControllerList.newInstance().getTestPlanController().
            operationChangeProfileId(profileId);
}//GEN-LAST:event_cmbProfilesİtemStateChanged

private void cmbTestBedsİtemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cmbTestBedsİtemStateChanged
    int testBedId = 0;
    if (cmbTestBeds.getSelectedItem() != null && !cmbTestBeds.getSelectedItem().
            toString().equals("")) {
        Entry entry = (Entry) cmbTestBeds.getSelectedItem();
        testBedId = Integer.parseInt(entry.getKey().toString());
    }
    TestDesignControllerList.newInstance().getTestPlanController().
            operationChangeTestBed(testBedId);
}//GEN-LAST:event_cmbTestBedsİtemStateChanged

    private void loadDutDevices() {
        cmbDutList.removeAllItems();
        cmbDutList.addItem("");
        List<DutRefined> duts = TestDesignControllerList.newInstance().
                getTestPlanController().operationGetDutList();
        if (duts != null) {
            if (hDuts == null) {
                hDuts = new HashMap<Integer, String>();
            } else {
                hDuts.clear();
            }
            for (int i = 0; i < duts.size(); i++) {
                hDuts.put(duts.get(i).getId(), duts.get(i).getName());
            }
            if (hDuts != null) {
                Iterator iter = hDuts.entrySet().iterator();
                while (iter.hasNext()) {
                    cmbDutList.addItem(new AtofEntryItem((Map.Entry) iter.next()));
                }
            }
        }
        cmbDutList.repaint();
        cmbDutList.updateUI();


    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel browserSpeedLabel;
    private javax.swing.JSpinner browserSpeedSpinner;
    private java.awt.Choice browserTypeChoice;
    private javax.swing.JLabel browserTypeLabel;
    private javax.swing.JButton closeButton;
    private javax.swing.JComboBox cmbDutList;
    private javax.swing.JComboBox cmbLanguages;
    private javax.swing.JComboBox cmbPcList;
    private javax.swing.JComboBox cmbProfiles;
    private javax.swing.JComboBox cmbTestBeds;
    private javax.swing.JLabel descriptionLabel;
    private javax.swing.JTextField descriptionTextField;
    private javax.swing.JLabel dutLabel;
    private javax.swing.JLabel dutLabel1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JTextField nameTextField;
    private javax.swing.JLabel pcLabel;
    private javax.swing.JButton saveButton;
    private javax.swing.JLabel timeoutLabel;
    private javax.swing.JTextField timeoutTextField;
    private javax.swing.JTextField txtDutIp;
    // End of variables declaration//GEN-END:variables

    public void modelPropertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals(TestPlanController.SHOW_ADD_TEST_PLAN)) {
            this.centerParent();
        } else if (evt.getPropertyName().equals(TestPlanController.CHANGE_NAME)) {
            this.nameTextField.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(TestPlanController.CHANGE_TIMEOUT)) {
            this.timeoutTextField.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(TestPlanController.CHANGE_DUT)) {
            Object key = evt.getNewValue();
            Iterator iter = hDuts.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (entry.getKey().toString().equals(key.toString())) {
                    cmbDutList.setSelectedItem(entry);
                    break;
                }
            }
            loadLanguages();
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_BROWSER_SPEED)) {
            //this.browserSpeedSpinner.setValue((Integer)evt.getNewValue());
            ((JSpinner.NumberEditor) browserSpeedSpinner.getEditor()).getTextField().setText(((Integer) evt.getNewValue()).toString());
            //System.out.println(this.getClass()+":CHANGE_BROWSER_SPEED");
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_BROWSER_TYPE)) {
            this.browserTypeChoice.select(evt.getNewValue().toString().substring(
                    1));
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_DESCRIPTION)) {
            this.descriptionTextField.setText(evt.getNewValue().toString());
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_DUT_IP)) {
            txtDutIp.setText((String) evt.getNewValue());
        } else if (evt.getPropertyName().equals(TestPlanController.CHANGE_PC)) {
            Object key = evt.getNewValue();
            Iterator iter = hTestPcs.entrySet().iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (entry.getKey().toString().equals(key.toString())) {
                    cmbPcList.setSelectedItem(entry);
                    break;
                }
            }
            loadDutDevices();
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_LANGUAGE_ID)) {
            Object key = evt.getNewValue();
            Iterator iter = hLanguages.entrySet().
                    iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (entry.getKey().toString().equals(key.toString())) {
                    cmbLanguages.setSelectedItem(entry);
                    break;
                }
            }
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_PROFILE_ID)) {
            Object key = evt.getNewValue();
            Iterator iter = hProfiles.entrySet().
                    iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (entry.getKey().toString().equals(key.toString())) {
                    cmbProfiles.setSelectedItem(entry);
                    break;
                }
            }
        } else if (evt.getPropertyName().equals(
                TestPlanController.CHANGE_TEST_BED)) {
            Object key = evt.getNewValue();
            Iterator iter = hTestBeds.entrySet().
                    iterator();
            while (iter.hasNext()) {
                Map.Entry entry = (Map.Entry) iter.next();
                if (entry.getKey().toString().equals(key.toString())) {
                    cmbTestBeds.setSelectedItem(entry);
                    break;
                }
            }
        }
    }
}
